//
//  ByUICardAndPayIndexVC.m
//  chequanbao_ios
//
//  Created by pro on 17/5/6.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUICardAndPayIndexVC.h"
// cell
#import "ByCellQRCode.h"
#import "ByCellCard.h"

// view
#import "ByViewCardAndPayHeader.h"

// other
#import "MKQRCode.h"
#import "FTPopOverMenu.h"
#import "ZKBombView.h"
#import "ZKShareView.h"

// model
#import "ByModelCardAndPay.h"

// api
#import "ByApiCardAndPay.h"


@interface ByUICardAndPayIndexVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) ByViewCardAndPayHeader *headerView;

// data
@property (nonatomic, strong) ByModelCardAndPay *model;
@end

@implementation ByUICardAndPayIndexVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"名片/支付";
    [self initTableview];
    self.table.backgroundColor = [UIColor clearColor];
    [self addBYItemviews:@[@"card_dian"] mode:ByNavigationItemModeImage position:ByNavigationItemPositionModeRight];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self fetchData];

}

- (void)viewSafeAreaInsetsDidChange{
    [super viewSafeAreaInsetsDidChange];
    self.table.top = self.view.safeAreaInsets.top;
    self.table.height = self.view.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom;
}

#pragma mark -- 获取数据
// 获取邀请码
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    ByModelUser *user = [ByCacheUser getUserInfo];
    model.code_type = user.account_id;
    model.account_id = user.uid;
    if (user.invite_code.length != 0) {
        self.model = [[ByModelCardAndPay alloc] init];
        self.model.invite_code = user.invite_code;
        self.model.url = [NSString stringWithFormat:@"%@index/index/invite_code/%@/uid/%@.html",BOYE_WEB_BASE_URL,user.invite_code,BY_USER.uid];
        [self.table reloadData];
    }
    else{
        [ByApiCardAndPay getInviteCode:model success:^(ByNetBaseResult *result) {
            if ([result.data isKindOfClass:[NSDictionary class]]) {
                self.model = [[ByModelCardAndPay alloc] initContentWithDic:result.data];
                self.model.url = [NSString stringWithFormat:@"%@index/index/invite_code/%@/uid/%@.html",BOYE_WEB_BASE_URL,self.model.invite_code,BY_USER.uid];
                user.invite_code = self.model.invite_code;
                user.url = self.model.url;
                [ByCacheUser setUserInfo:user];
                [self.table reloadData];
            }
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showOnlyText:result.msg];
        }];

    }
}

#pragma mark -- 创建视图
- (void)initTableview{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStyleGrouped];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.delegate = self;
    _table.dataSource = self;
    _table.estimatedRowHeight = 0;
    _table.estimatedSectionHeaderHeight = 0;
    _table.estimatedSectionFooterHeight = 0;
    _table.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:_table];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ByCellQRCode *cell = [ByCellQRCode cellWithTableView:tableView];
        cell.model = self.model;
        MKQRCode *code = [[MKQRCode alloc] init];
        if (self.model.invite_code.length != 0) {
            NSString *codeStr = [NSString stringWithFormat:@"%@index/index/invite_code/%@/uid/%@.html",BOYE_WEB_BASE_URL,self.model.invite_code,BY_USER.uid];
            [code setInfo:codeStr withSize:(SCREEN_WIDTH - 35*2) - 2*20];
            cell.outPutImg = [code generateImage];
        }
        return cell;

    }
    else{
        ByCellCard *cell = [ByCellCard cellWithTableView:tableView];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 140 + (SCREEN_WIDTH - 35*2) - 2*20;
//        return SCREEN_HEIGHT - 64 - 49 - 80 - 20;
    }
    return 80;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 10;
    }
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 10;
    }
    return 0.0001;
//    return 20;
}
#pragma mark -- 右导航按钮点击事件
-(void)rightItemsAction:(NSInteger)rightIndex{
   
    [FTPopOverMenu showFromSenderFrame:CGRectMake(SCREEN_WIDTH - 90, 50, 80, 130 - 40) point:CGPointMake(65, 0) withMenu:@[@"分享",@"使用说明"] imageNameArray:nil doneBlock:^(NSInteger selectedIndex){
        if (selectedIndex == 0) {
            // 创建弹框视图
            ZKBombView *moreview = [[ZKBombView alloc] init];
//            if (@available(iOS 11.0,*)) {
//                moreview.positionMode = ZKBombPositionModeIphoneX;
//
//            }else{
            moreview.positionMode = ZKBombPositionModeBottom;

//            }
            moreview.damping = 0.8;
            
            // 创建内容视图
            ZKShareView *contentview = [[ZKShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
            moreview.contentView = contentview;
            // 分享链接
            contentview.invite_code = self.model.invite_code;
            [moreview show];

        }
        else{
            [ByCoreToolAlert showOnlyText:@"使用说明"];
        }
    }dismissBlock:^{
        
    }];

}

@end
